import {
  GetCurrentDateReturn,
  GetCollectionValues
} from '../interfaces'
import { isDataType } from './is'
export * from './is'
export * from './subscription'

// 获取当前日期的格式化时间
export function getCurrentDate(): GetCurrentDateReturn {
  const date = new Date();
  const y = date.getFullYear();
  const m = date.getMonth() + 1;
  const d = date.getDate();
  const h = date.getHours();
  const mm = date.getMinutes();
  const ss = date.getSeconds();
  return {
    y,
    m,
    d,
    h,
    mm,
    ss,
    date: `${y}-${m}-${d}`,
    dateTime: `${y}-${m}-${d} ${h}:${mm}:${ss}`
  }
}
// 解析日期字符串
export function parseDate(val: any): GetCurrentDateReturn {
  let date = [];
  if (isDataType(val, 'String')) {
    val = val.replace(/[\-|/|\s]/g, ':')
    date = val.split(':');
  }
  const [y, m, d, h = 0, mm = 0, ss = 0] = date;
  return {
    y: Number(y),
    m: Number(m),
    d: Number(d),
    h: Number(h),
    mm: Number(mm),
    ss: Number(ss),
    date: `${y}-${m}-${d}`,
    dateTime: `${y}-${m}-${d} ${h}:${mm}:${ss}`
  }
}
export function getCollectionValues(params: GetCollectionValues = {start: 1, end: 12, format: 2, order: 'asc'}): string[] {
  let values = [];
  const { start=1, end=12, format=2, order='asc' } = params;
  for (let i = start; i <= end; i++) {
    values.push(String(i).padStart(format, '0'))
  }
  if (order === 'desc') {
    return values.reverse();
  }
  return values;
}